#include "timer.h"
#include "music.h"

volatile int time_tick;
volatile int sec_tick;

void timer_init(int timer, int mode)
{
	if (timer == 2)
	{
		char val = inportb(SPEAKER_CTRL);
		outportb(SPEAKER_CTRL, val | TIMER2_ENABLE);
		outportb(TIMER_CTRL, mode | (timer << 6));	
		
	}
}

int get_divider(float freq)
{
	if(!freq) return -1;
	return (int)(TIMER_CLK / freq + 0.5)*100;
}

void timer_load(int timer, int value)
{
	outportb(TIMER_0 | timer, LSB(value));
	outportb(TIMER_0 | timer, MSB(value));
}

void mili_sleep(int mili)
{
	int fim = mili + time_tick;
	while(time_tick < fim)
	{}
}

void sec_sleep(int secs)
{
	int fim = secs + sec_tick;
	while(sec_tick < fim)
	{}
}
